package com.zhongjian.technologycal.mall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.TestAdapter;

import java.util.List;

/**
 * Created by wei on 2018/1/17.
 */

public class MemberAreaAdaper extends BaseAdapter {
    private Context context;
    private List<TestAdapter> listItems;
    private LayoutInflater listContainer;

    public MemberAreaAdaper(Context context, List<TestAdapter> listItems) {
        this.context = context;
        listContainer = LayoutInflater.from(context);
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            listItemView = new ListItemView();
            convertView = listContainer.inflate(R.layout.items_members_areas,
                    null);
            listItemView.member_iv_photo = (ImageView) convertView
                    .findViewById(R.id.member_iv_photo);
            listItemView.member_tv_product_name = (TextView) convertView
                    .findViewById(R.id.member_tv_product_name);
            listItemView.member_tv_price = (TextView) convertView
                    .findViewById(R.id.member_tv_price);
            listItemView.member_tv_credits = (TextView) convertView
                    .findViewById(R.id.member_tv_credits);
            listItemView.elecnum_tv_count = (TextView) convertView.findViewById(R.id.elecnum_tv_count);

            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }
          //将数据填充到具体的view中
        notifyDataSetChanged();
        listItemView.member_tv_product_name.setText(listItems.get(position).getProductname());
        if( listItems.get(position).getProductphotos().size() > 0){
            Picasso.get()
                    .load(BaseContacts.BASE_URL +
                            listItems.get(position).getProductphotos().get(0).getPhoto())
                    .into(listItemView.member_iv_photo);
        }
        listItemView.member_tv_price.setText("" + listItems.get(position).getPrice());
        listItemView.member_tv_credits.setText("" + listItems.get(position).getElecnum() / 2);
        listItemView.elecnum_tv_count.setText(""+ listItems.get(position).getElecnum() + "积分");
        return convertView;
    }
    public final class ListItemView {
        private ImageView member_iv_photo;
        private TextView member_tv_product_name;
        private TextView member_tv_price;
        private TextView member_tv_credits;
        private TextView elecnum_tv_count;
    }

}
